package com.cybertron.ironhide.search.common.ftl;

import java.util.List;

import org.apache.commons.lang.StringUtils;

import freemarker.template.TemplateMethodModelEx;
import freemarker.template.TemplateModelException;

public class EncodeUrlMethod implements TemplateMethodModelEx {

	private static final String BLANK_PATTERN = "[^0-9a-zA-Z]";

	@SuppressWarnings("rawtypes")
	@Override
	public Object exec(List args) throws TemplateModelException {
		if (args == null || args.size() == 0) {
			return null;
		}
		String input = args.get(0).toString();
		if (StringUtils.isNotBlank(input)) {
			return input.replaceAll(BLANK_PATTERN, "-");
		} else {
			return input;
		}
	}
}
